package com.sd.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.MessageFormat;

import android.util.Log;

public class HttpUtil {
    private static final String TAG = "HttpUtil";
	private static String url ;//= BaseActivity.appserverPath + "appDemoServlet?sessionId=";

	public static final String DEFAULT_CHARSET = "UTF-8";
	
	
	/**
	 * 从指定的sessionId读取数据.
	 * @param sessionId
	 * @param charSet  指定的编码
	 * @param params
	 * @return
	 */
	public static String readStringFromUrl(String appId, String sessionId, String charSet,
			Object... params) {
		String uri = "";
		if (sessionId == null || sessionId.equals("")) {
			return "sessionId 为空";
		} else {
			sessionId = URLEncoder.encode(sessionId);
			//if (BaseActivity.appserverPath == null) {
				//url = "http://221.130.195.133:8081/appdemo/appDemoServlet?sessionId=";
				url = "http://221.181.69.31:9201/apps/sdologin.do?sessionId=";
			//}
			uri = url + sessionId;
		}
		
		if (appId == null || appId.equals("")) {
			return "appId 为空";
		} else {
			uri = uri + "&appId=" + appId;
		}
		
		return readStringFromUrl(uri, charSet, 30000, params);

	}
	
	/**
	 * 从指定的sessionId读取数据.
	 * @param sessionId
	 * @param charSet  指定的编码
	 * @param params
	 * @return
	 */
	public static String readStringFromUrl(String sessionId, String charSet,
			Object... params) {
		if (sessionId == null || sessionId.equals("")) {
			return "sessionId 为空";
		} else {
			sessionId = URLEncoder.encode(sessionId);
			return readStringFromUrl(url + sessionId, charSet, 30000, params);
		}

	}

	/**
	 * 从指定的URL地址读取数据.
	 * 
	 * @param strUrl
	 *            url地址,若有参数，格式为?param1={0}&param2={1}
	 * @param charSet
	 *            指定的编码
	 * @param timeout
	 *            超时时间
	 * @param params
	 *            参数
	 * @return 指定编码的字符串
	 */
	public static String readStringFromUrl(String strUrl, String charSet,
			int timeout, Object... params) {
		Log.d(TAG, "appserver url is : " + strUrl);
		if (charSet == null || charSet.equals("")) {
			charSet = DEFAULT_CHARSET;
		}
		String ret = null;
		InputStream in = null;
		ByteArrayOutputStream out = null;
		try {
			in = getInputStreamFromUrl(strUrl, timeout, params);
			if (in == null) {
				return ret;
			}
			out = new ByteArrayOutputStream(1024);
			byte[] temp = new byte[1024];
			int size = 0;
			while ((size = in.read(temp)) != -1) {
				out.write(temp, 0, size);
			}
			in.close();
			byte[] content = out.toByteArray();
			ret = new String(content, charSet);
		} catch (MalformedURLException me) {
			Log.e(TAG, "readStringFromUrl MalformedURLException", me);
		} catch (IOException ioe) {
			Log.e(TAG, "readStringFromUrl IOException", ioe);
		} finally {
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
			}
		}
		Log.i(TAG, "url[" + strUrl + "]ret[" + ret + "]");
		return ret;
	}

	/**
	 * 从指定的URL地址读取数据流.
	 * 
	 * @param strUrl
	 *            url地址,若有参数，格式为?param1={0}&param2={1}
	 * @param params
	 *            参数
	 * @param timeout
	 *            超时时间
	 * @return 数据流
	 */
	public static InputStream getInputStreamFromUrl(String strUrl, int timeout,
			Object... params) {
		InputStream in = null;
		try {
			strUrl = MessageFormat.format(strUrl, params);
			URL url = new URL(strUrl);
			URLConnection urlConnection = url.openConnection();
			urlConnection.setConnectTimeout(timeout);
			urlConnection.setReadTimeout(timeout);
			in = urlConnection.getInputStream();
		} catch (MalformedURLException me) {
			Log.e(TAG, "readStringFromUrl MalformedURLException", me);
		} catch (IOException ioe) {
			Log.e(TAG, "readStringFromUrl IOException", ioe);
		}
		return in;
	}
	
}
